//
//  DestinNextCtrl.m
//  LvXingPai
//
//  Created by _Junxee on 16/8/10.
//  Copyright © 2016年 _Junxee. All rights reserved.
//

#import "DestinNextCtrl.h"
#import "DestinNextCell.h"
#import "IntroduceCtrl.h"
#import "JourneyCtrl.h"
#import "GuideCtrl.h"
#import "TripCtrl.h"
#import "FoodCtrl.h"
#import "ShoppingCtrl.h"


@interface DestinNextCtrl ()

@property(nonatomic,strong) NSMutableArray *dataArr;
@property(nonatomic,strong) UILabel * StoreLabel;

@end

@implementation DestinNextCtrl

- (NSMutableArray *)dataArr
{
    if (_dataArr == nil) {
        _dataArr = [[NSMutableArray alloc]init];
    }
    return _dataArr;
}

//简介按钮
- (IBAction)IntroBtn:(UIButton *)sender {
    IntroduceCtrl * intro = [self.storyboard instantiateViewControllerWithIdentifier:@"intro"];
    intro.Bmodel = self.Amodel;
    [self.navigationController pushViewController:intro animated:YES];
}

//指南按钮
- (IBAction)GuiBtn:(UIButton *)sender {
    GuideCtrl * guideVC = [[GuideCtrl alloc]init];
    guideVC.destinModel = self.Amodel;
    [self.navigationController pushViewController:guideVC animated:YES];
}

//交通按钮
- (IBAction)TrafficBtn:(UIButton *)sender {
    TrafficCtrl *trafficVC = [[TrafficCtrl alloc]init];
    trafficVC.tModel = self.Amodel;
    [self.navigationController pushViewController:trafficVC animated:YES];
}
//景点按钮
- (IBAction)ScenicBtn:(UIButton *)sender {
    ScenicTableViewCtrl * sCtrl = [self.storyboard instantiateViewControllerWithIdentifier:@"sceninc"];
    sCtrl.ScenModel = self.Amodel;
    [self.navigationController pushViewController:sCtrl animated:YES];
}
//行程按钮
- (IBAction)JouneyBtn:(UIButton *)sender {
    JourneyCtrl *jCtrlVC = [self.storyboard instantiateViewControllerWithIdentifier:@"jstory"];
    jCtrlVC.jModel = self.Amodel;
    [self.navigationController pushViewController:jCtrlVC animated:YES];
}
//游记按钮
- (IBAction)TripBtn:(UIButton *)sender {
    TripCtrl *tripVC = [self.storyboard instantiateViewControllerWithIdentifier:@"tripV"];
    tripVC.tModel = self.Amodel;
    [self.navigationController pushViewController:tripVC animated:YES];
}
//美食按钮
- (IBAction)FoodBtn:(UIButton *)sender {
    FoodCtrl *foodVC = [self.storyboard instantiateViewControllerWithIdentifier:@"foodVC"];
    foodVC.foodModel = self.Amodel;
    [self.navigationController pushViewController:foodVC animated:YES];
}
- (IBAction)shopBtn:(UIButton *)sender {
    ShoppingCtrl *shopVC = [self.storyboard instantiateViewControllerWithIdentifier:@"ShopVC"];
    shopVC.shopModel = self.Amodel;
    [self.navigationController pushViewController:shopVC animated:YES];
}

//把上一个界面的数据传过来这个界面使用
-(void)setAmodel:(DestinModel *)Amodel{
    _Amodel = Amodel;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setActionInTopImage];
    self.tujiView.layer.borderColor = [[UIColor whiteColor] CGColor];
    self.tujiView.layer.borderWidth = 1.0;
    self.tujiView.clipsToBounds = YES;
    self.tujiView.layer.cornerRadius = 3;

    //显示导航栏
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    
    //设置状态栏样式
    [self preferredStatusBarStyle];
    
    //设置导航栏标题
    self.title = _Amodel.zhName;
    
    //设置导航栏的按钮样式
    [self setBarButStyle];
    

    [self loadTopImageViewAndLabelName];
    [self loadData];
}


-(void)viewWillAppear:(BOOL)animated{
    self.tabBarController.tabBar.hidden = NO;
}

//给顶部大图添加点击事件
-(void)setActionInTopImage{
    
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
    
    [self.TopImageView addGestureRecognizer:tapGesture];
}

//顶部大图点击事件实现
-(void)tapAction:(UITapGestureRecognizer *)tap{
    
    AlbumViewController * AlbumVC = [[AlbumViewController alloc]init];
    AlbumVC.Dmodel = self.Amodel;
    [UIView  beginAnimations:nil context:NULL];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
    [UIView setAnimationDuration:1.5];
    [self.navigationController pushViewController:AlbumVC animated:NO];
    [UIView setAnimationTransition:UIViewAnimationTransitionNone forView:self.navigationController.view cache:NO];
    [UIView commitAnimations];

}


//设置导航栏的按钮样式和按钮事件
-(void)setBarButStyle{
    UIBarButtonItem *leftBtn = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"common_icon_navigation_back_normal"] style:UIBarButtonItemStylePlain target:self action:@selector(backAction:)];
    
    self.navigationItem.leftBarButtonItem = leftBtn;
}

//自定义按钮事件
-(void)backAction:(UIBarButtonItem *)sender{
    [self.navigationController popViewControllerAnimated:YES];
}

-(UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
}

//加载顶部图片
-(void)loadTopImageViewAndLabelName{
    [self.TopImageView sd_setImageWithURL:[NSURL URLWithString:self.Amodel.url]];
    self.ImageUpLabel.text = self.Amodel.zhName;
    self.ImageDownLabel.text = self.Amodel.enName;
}

//加载数据
-(void)loadData{
    
    AFHTTPRequestOperationManager * Manger = [AFHTTPRequestOperationManager manager];
    Manger.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    [Manger GET:[NSString stringWithFormat:@"http://api.lvxingpai.com/app/marketplace/commodities?count=3&locality=%@&start=0",_Amodel.pushId] parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {

        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        NSArray *arr = dict[@"result"];
        for (NSDictionary *dic in arr) {
            DestinNextModel *NextModel = [[DestinNextModel alloc]init];
            NextModel.titleName = dic[@"title"];
            NextModel.url = dic[@"cover"][@"url"];
            NextModel.name = dic[@"seller"][@"name"];
            NextModel.salesVolume = [[[NSNumberFormatter alloc]init] stringFromNumber:dic[@"salesVolume"]];
            NextModel.marketPrice = [[[NSNumberFormatter alloc]init] stringFromNumber:dic[@"marketPrice"]];
            NextModel.price = [[[NSNumberFormatter alloc]init] stringFromNumber:dic[@"price"]];
            
            [self.dataArr addObject:NextModel];
        }
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
        });
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"f");
    }];

}

#pragma mark -- tableviewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.dataArr.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   
    DestinNextCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    DestinNextModel * model = self.dataArr[indexPath.row];
    cell.titleLabel.text = model.titleName;
    cell.OldPriceLabel.text = model.marketPrice;
    cell.NewPriceLaebl.text = model.price;
    cell.sellerLabel.text = model.name;
    cell.BuyCountLabel.text = model.salesVolume;
    [cell.LeftImageView sd_setImageWithURL:[NSURL URLWithString:model.url]];
    return cell;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    UIView * headView = [[UIView alloc]initWithFrame:CGRectMake(0, 20, SCREEN_W, 40)];
    UILabel *leftLabel = [[UILabel alloc]initWithFrame:CGRectMake(8, 9, 80, 25)];
    leftLabel.text = @"当地玩乐";
    leftLabel.font = [UIFont fontWithName:@"Helvetica" size:16.5];
    leftLabel.textColor = [UIColor darkGrayColor];
    [headView addSubview:leftLabel];
    
    UIImageView *RightImageV = [[UIImageView alloc]initWithFrame:CGRectMake(335, 12, 11, 11)];
    RightImageV.image = [UIImage imageNamed:@"icon_goodsCount_green"];
    [headView addSubview:RightImageV];
    
    UIView * bottomView = [[UIView alloc]initWithFrame:CGRectMake(0, 39, SCREEN_W, 1)];
    bottomView.backgroundColor = [UIColor colorWithRed:211/255.0 green:237/255.0 blue:205/255.0 alpha:1.0];
    [headView addSubview:bottomView];
    
    self.StoreLabel = [[UILabel alloc]initWithFrame:CGRectMake(352, 12, 15, 12)];
    self.StoreLabel.font = [UIFont fontWithName:@"Helvetica" size:13];
    self.StoreLabel.text = _Amodel.commoditiesCnt;
    self.StoreLabel.textColor = [UIColor darkGrayColor];
    [headView addSubview:self.StoreLabel];
    return headView;
    
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 40;
}



@end
